/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package android.web.async;

import android.app.ContextoAndroid;
import android.app.CustomListViewAdapter;
import android.app.Mensaje;
import android.app.ProgressDialog;
import android.app.RowItem;
import android.os.AsyncTask;
import android.reasons.R;
import android.web.service.IPServer;
import android.web.service.WebService;
import android.widget.ListView;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

public class WebAmigos extends AsyncTask<String, Integer, JSONArray> 
{
    private ArrayList<RowItem> rowItems = new ArrayList<RowItem>();
    private ProgressDialog dialog;
    private CustomListViewAdapter adapter;
    private ListView lvAmigos;
    
    public WebAmigos(ListView lv)
    {
        super();
        this.lvAmigos = lv;
    }
    
    public ArrayList<RowItem> getRowItems() {
        return rowItems;
    }

    public void setRowItems(ArrayList<RowItem> rowItems) {
        this.rowItems = rowItems;
    }
    
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(ContextoAndroid.context);
        dialog.setIndeterminate(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.setMessage("Buscando...");
        dialog.show();
    }
    @Override
    protected JSONArray doInBackground(String... params) 
    {
        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("search",params[0]));
            this.publishProgress(10);
            JSONArray array = WebService.getJSONArray(nameValuePairs, "Usuarios", "Controladores/searchUsuarios.php");
            this.publishProgress(40);
            this.publishProgress(100);
            return array;
        } catch (Exception ex) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(JSONArray result) 
    {
        fill(result);
        dialog.dismiss();
        
    }

    private void fill(JSONArray result)
    {
        rowItems.clear();
        try
        {
            for(int x=0;x<result.length();x++)
            {
                JSONArray js = result.getJSONArray(x);
                int r=-1;
                String nombre = js.getString(++r);
                String correo = js.getString(++r);
                String tipo = js.getString(++r);
                String foto = js.getString(++r);
                String http = IPServer.fullServer+"Fotos/uploads/thumbs/"+foto;
                RowItem item = new RowItem(http, nombre,correo,"");
                rowItems.add(item);
            }
           
        }
        catch(Exception e){Mensaje.Mostrar(""+e);}
        adapter = new CustomListViewAdapter(ContextoAndroid.context,R.layout.listitem, rowItems);
        lvAmigos.setAdapter(adapter); 
    }

    @Override
    protected void onProgressUpdate(Integer... values) 
    {
        dialog.setMessage("Cargando...");
        dialog.setProgress(values[0]);
    }
}
